package com.contact;


import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.android.ocean.util.common.StringUtil;
import com.android.ocean.util.common.TranHelper;
import com.http.FriendApplyListTask;
import com.matang.R;
import com.modle.ApplyFriends;
import com.modle.User;
import com.util.AppUtil;
import com.util.UserUtil;
import com.view.XFriendListView;

public class TabFriendActivity extends BaseActivity implements OnClickListener {
	RelativeLayout seaContainer, addContainer;
	XFriendListView txlListView;
	User user;


	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_tab_txl);
		init();
	}

	void init() {
		mContext = TabFriendActivity.this;
		user = UserUtil.readUser();
		if (user == null) {  
			Toast.makeText(getApplicationContext(), "请先登录", 0).show();
			TabFriendActivity.this.finish();
			return;
		}
		findViews();
		initPaneData();

	}

	@Override
	protected void onResume() {

		super.onResume();
		if (user == null) {  
			Toast.makeText(getApplicationContext(), "请先登录", 0).show();
			TabFriendActivity.this.finish();
			return;
		}
		txlListView.initData(UserUtil.readUser().id);
	}
	@Override
	protected void onRestart() {
		
		txlListView.initData(UserUtil.readUser().id);
		super.onRestart();
	}

	public void initPaneData() {
		header.headRightTv.setBackgroundResource(R.drawable.btn_bg);
	//	getApplyList();
		header.headTitleTv.setText("通讯录");
		header.headLeftTv.setVisibility(View.VISIBLE);
		header.headRightTv.setVisibility(View.GONE);

		header.headRightTv.setOnClickListener(this);
		seaContainer.setOnClickListener(this);
		addContainer.setOnClickListener(this);
		header.headRightTv.setText("申请(0)条");
		header.headLeftTv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				finish();
				
			}
		});
	}

	@Override
	protected void findViews() {
		super.findViews();
		seaContainer = (RelativeLayout) findViewById(R.id.sear_con);
		addContainer = (RelativeLayout) findViewById(R.id.add_con);
		txlListView = (XFriendListView) findViewById(R.id.txl_pane_listview);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.sear_con: {
			Intent intent = new Intent(mContext, UserSearchActivity.class);
			startActivity(intent);
			break;
		}
		case R.id.add_con: {
			Intent intent = new Intent(mContext, UserAddActivity.class);
			startActivity(intent);
			break;
		}
		case R.id.headRight: {
			if (!StringUtil.isListNull(applyList)) {
				Bundle b = new Bundle();
				b.putSerializable("applyList", applyList);
				TranHelper.toAct(mContext, FriendApplyListActivity.class, b);
				applyList = null;
				updateRightBtn();
			}
			break;
		}

		}

	}

	final int FETCH_APPFRIENDS = 11;
	final int TIME_PER = 1000*3;
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == FETCH_APPFRIENDS) {

			//	getApplyList();

			}
		}

	};
	ArrayList<ApplyFriends> applyList=new ArrayList<ApplyFriends>();// 申请用户列表

	void getApplyList() {
		new FriendApplyListTask(falCall).execute(user.id);
		handler.sendEmptyMessageDelayed(FETCH_APPFRIENDS, TIME_PER);
	}

	FriendApplyListTask.CallBack falCall = new FriendApplyListTask.CallBack() {

		@Override
		public void procError(String msg) {
			AppUtil.tsMsg(mContext, msg);
		}

		@Override
		public void procApplyFriendsListData(ArrayList<ApplyFriends> dataList) {
			applyList = dataList;
			updateRightBtn();
		}
	};

	void updateRightBtn() {
		if (!StringUtil.isListNull(applyList)) {
			header.headRightTv.setText("申请(" + applyList.size() + ")条");

		}
	}
}
